package com.uduew.sjd.view.xpop

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.kongzue.dialogx.dialogs.WaitDialog
import com.lxj.xpopup.core.CenterPopupView
import com.uduew.sjd.R
import com.uduew.sjd.config.Const
import com.uduew.sjd.utils.ResUtil

class LoanAgreementXpop(context: Context) : CenterPopupView(context), View.OnClickListener {
    var privacyAgreeTv: TextView? = null
    var privacyDisagreeTv: TextView? = null
    private var webView: WebView? = null
    private var onAgreeClickListener: OnAgreeClickListener? = null

    fun setOnAgreeClickListener(listener: OnAgreeClickListener?) {
        this.onAgreeClickListener = listener
    }

    interface OnAgreeClickListener {
        fun onAgreeClick()
    }

    override fun getImplLayoutId(): Int {
        return R.layout.xpop_loan_agreement
    }

    override fun onCreate() {
        super.onCreate()
        privacyAgreeTv = findViewById<TextView>(R.id.privacy_agree_tv)
        privacyAgreeTv!!.setOnClickListener(this)
        privacyDisagreeTv = findViewById<TextView>(R.id.privacy_disagree_tv)
        privacyDisagreeTv!!.setOnClickListener(this)

        webView = findViewById<WebView>(R.id.wv_content)
        webView?.webViewClient = WebViewClient()
        webView?.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
        val webSettings = webView?.settings
        webSettings.let {
            if (it != null) {
                it.javaScriptEnabled = true
                it.defaultTextEncodingName = "UTF-8"
                it.setSupportZoom(true)
                it.builtInZoomControls = true
                it.displayZoomControls = false
                it.useWideViewPort = true
                it.loadWithOverviewMode = true
            }
        }

        webView?.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                WaitDialog.show(ResUtil.getString(R.string.loading))
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                WaitDialog.dismiss()
            }
        }
        webView?.loadUrl(Const.PRIVACY_URL)
    }

    override fun onClick(v: View) {
        if (v.getId() == R.id.privacy_agree_tv) {
            if (onAgreeClickListener != null) {
                onAgreeClickListener!!.onAgreeClick()
            }
            dismiss()
        }
        if (v.getId() == R.id.privacy_disagree_tv) {
            dismiss()
        }
    }
}
